package ldh.msg.ui.pref;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import ldh.msg.helper.MsgConstants;
import ldh.msg.ui.MessengerUI;
import ldh.msg.ui.helper.MsgUIUtils;

public class MsgPreference {

	private JDialog prfDialog;
	private JSplitPane splitPane;
	private JScrollPane leftScrollPane;
	private JPanel bottomPanel;
	private JTree leftTree;
	private JPanel rightPanel, applyPanel;
	private JPanel aboutPanel;
	private JPanel blockNetworkPanel, themePanel;

	private Icon openTreeIcon = new ImageIcon(MsgConstants.DIR_DSP + "nav_down_right_green.png");
	private Icon closedTreeIcon = new ImageIcon(MsgConstants.DIR_DSP + "nav_right_green.png");
	private Icon leafTreeIcon = new ImageIcon(MsgConstants.DIR_DSP + "media_play_green.png");
	  
	private java.awt.FlowLayout rightAlignFlowLayOut = null;
	private JButton applyBtn = null;
	private JButton okBtn = null;
	private JButton cancelBtn = null;
		
	private GeneralPanel generalPanel = null;
	private AbsentPanel absentPanel = null;
	private AppearancePanel appearancePanel = null;
	private SoundPanel soundPanel = null;
	private NetworkPanel networkPanel = null;
	
	private static MsgPreference prf = null;
	
	private MsgPreference() {
		initialize();
	}
	
	public static MsgPreference getInstance() throws Exception {
		if (prf == null) {
			synchronized (MsgPreference.class) {
				prf = new MsgPreference();
			}
		}
		return prf;
	}
	
	private void initialize() {
		try {			
			prfDialog = new JDialog(MessengerUI.getInstance().getMainFrame(), true);
			prfDialog.setTitle("Preferences");
			prfDialog.setIconImage(new ImageIcon(MsgConstants.DIR_DSP + "options.png").getImage());
			prfDialog.setLocation(MessengerUI.getInstance().getMainFrame().getLocation());
			prfDialog.setSize(500, 400);
	        prfDialog.setVisible(false);
	        prfDialog.setResizable(false);
	        prfDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	        
	        makeGeneralPanel();
	        makeAboutPanel();
	        makeAbsentPanel();
	        makeAppearancePanel();
	        makeBlockNetworkPanel();
	        makeNetworkPanel();
	        makeSoundPanel();
	        makeThemePanel();
	        
	        makeBottomPanel();
			DefaultMutableTreeNode root =  new DefaultMutableTreeNode("Root");
	        DefaultMutableTreeNode child;
	        DefaultMutableTreeNode grandChild;
	        child = new DefaultMutableTreeNode("General");
            root.add(child);
            grandChild = new DefaultMutableTreeNode("Absent");
            child.add(grandChild);
            grandChild = new DefaultMutableTreeNode("Appearance");
            child.add(grandChild);
            grandChild = new DefaultMutableTreeNode("Sound");
            child.add(grandChild);
            grandChild = new DefaultMutableTreeNode("Theme");
            child.add(grandChild);
            
            child = new DefaultMutableTreeNode("Network");
            root.add(child);
            grandChild = new DefaultMutableTreeNode("Block Network");
            child.add(grandChild);
            
            child = new DefaultMutableTreeNode("About");
            root.add(child);            
            
            leftTree = new JTree(root);
            leftTree.setRootVisible(false);
            DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();
            renderer3.setOpenIcon(openTreeIcon);
            renderer3.setClosedIcon(closedTreeIcon);
            renderer3.setLeafIcon(leafTreeIcon);
  
            leftTree.setCellRenderer(renderer3);
            leftTree.setCellRenderer(renderer3);
            leftTree.setCellRenderer(renderer3);
            leftTree.addTreeSelectionListener(leftTreeSelectionListener);
	          
	        leftTree.addMouseListener(new MouseListener() {	      		
	      		public void mouseReleased(MouseEvent e) {	}	      		
	      		public void mousePressed(MouseEvent e) {}	      		
	      		public void mouseExited(MouseEvent e) {}	      		
	      		public void mouseEntered(MouseEvent e) {}
	      		public void mouseClicked(MouseEvent e) {
	                  	if (! leftTree.isCollapsed(leftTree.getSelectionPath())) {
	                  		leftTree.collapsePath(leftTree.getSelectionPath());
	                  	} else {
	                  		leftTree.expandPath(leftTree.getSelectionPath());
	                  	}
	      		}
	      	});        
	        
	        leftScrollPane = new JScrollPane(leftTree);
	        rightPanel = new JPanel();
	        rightPanel.setLayout(new BorderLayout());
	        rightAlignFlowLayOut = new java.awt.FlowLayout(FlowLayout.RIGHT, 1, 1);
	        applyPanel = new JPanel(rightAlignFlowLayOut);
	        applyPanel.setPreferredSize(new Dimension(350, 26));
	        applyBtn = MsgUIUtils.makeBtn("Apply", 45, 24);
	        applyBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					doApply();
				}
			});
	        applyPanel.add(applyBtn);
	        rightPanel.add(applyPanel, BorderLayout.SOUTH);
	        
	        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScrollPane, rightPanel);
			splitPane.setDividerSize(3);
			splitPane.setResizeWeight(0.5);
			splitPane.setDividerLocation(140);
			splitPane.setEnabled(false);			
			
	        prfDialog.add(splitPane, BorderLayout.CENTER);
	        prfDialog.add(bottomPanel, BorderLayout.SOUTH);
	        
	        leftTree.setSelectionRow(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void reload() {
		try {
//			makeAboutPanel();
//	        makeAbsentPanel();
//	        makeAppearancePanel();
//	        makeBlockNetworkPanel();
////	        makeBottomPanel();
//	        makeGeneralPanel();
//	        makeMyGroupPanel();
//	        makeMyNetworkPanel();
//	        makeNetworkPanel();
//	        makeSoundPanel();
//	        makeThemePanel();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setVisiblePrfFrame(boolean b) {
		prfDialog.setVisible(b);
	}

	private void makeBottomPanel() {
		bottomPanel = new JPanel();
		bottomPanel.setPreferredSize(new Dimension(490, 40));
		okBtn = new JButton("Ok");
		okBtn.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				doOk();
			}
		});
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent e) {
				doCancel();
			}
		});
		bottomPanel.add(okBtn);
		bottomPanel.add(cancelBtn);
	}
	
	private void makeGeneralPanel() throws Exception {
		generalPanel = new GeneralPanel();
		generalPanel.initialize();
	}
	
	private void makeAbsentPanel() throws Exception {
		absentPanel = new AbsentPanel();
		absentPanel.initialize();
	}
	
	private void makeAppearancePanel() throws Exception {
		appearancePanel  = new AppearancePanel();
		appearancePanel.initialize();
	}
	
	private void makeSoundPanel() throws Exception {
		soundPanel  = new SoundPanel();
		soundPanel.initialize();
	}

	private void makeThemePanel() {
		themePanel  = new JPanel(new GridLayout(6, 1));
	}

	private void makeNetworkPanel() throws Exception {
		networkPanel  = new NetworkPanel();
		networkPanel.initialize();
	}
		
	private void makeBlockNetworkPanel() {
		blockNetworkPanel  = new JPanel(new GridLayout(6, 1));
	}

	private void makeAboutPanel() {
		aboutPanel  = new JPanel(new GridLayout(10, 1));
		JLabel bugLabel1 = new JLabel(" Find Bug and come up with any idea, ");
		aboutPanel.add(bugLabel1);
		JLabel bugLabel2 = new JLabel(" Send to dhlee@koreainfobiz.com");
		aboutPanel.add(bugLabel2);
		aboutPanel.add(new JLabel());
		aboutPanel.add(new JLabel());
		JLabel trayIconLabel = new JLabel(" TrayIcon : Designed by Oh hyeon-kyeong");
		aboutPanel.add(trayIconLabel);
		JLabel dateLabel = new JLabel(" 2010-04-10");
		aboutPanel.add(dateLabel);
	}

	private void doApply() {
		try {
			String str = (String)leftTree.getSelectionPath().getLastPathComponent().toString();
			System.out.println("doApply str : " +  str);
			if (str.equals("General")) {
	      		generalPanel.doApply();
	      	} else if (str.equals("Appearance")) {
	      	} else if (str.equals("Absent")) {
	      		absentPanel.doApply();
	      	} else if (str.equals("Theme")) {
	      	} else if (str.equals("About")) {
	      	} else if (str.equals("Network")) {
	      		networkPanel.doApply();
	      	} else if (str.equals("Block Network")) {
	      	} else if (str.equals("Sound")) {
	      		soundPanel.doApply();
	      	}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void doOk() {
		prfDialog.setVisible(false);
	}
	
	private void doCancel() {
		prfDialog.setVisible(false);
	}
	
	private TreeSelectionListener leftTreeSelectionListener = new TreeSelectionListener() {			
		@Override
		public void valueChanged(TreeSelectionEvent e) {
          	if (e.getOldLeadSelectionPath() != null) {
	            if (e.getOldLeadSelectionPath().getLastPathComponent().toString().equals("Sound")) {
	          		if (soundPanel != null) {
	          			System.out.println(" stopPlayer ");
	          			soundPanel.stopPlayer();
	          		}
	          	}
          	}
          	String str = (String)leftTree.getSelectionPath().getLastPathComponent().toString();
          	rightPanel.removeAll();
          	if (str.equals("General")) {
          		rightPanel.add(generalPanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("Appearance")) {
          		rightPanel.add(appearancePanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("Absent")) {
          		System.out.println("Absent : " +  absentPanel.getComponentCount());          		
          		rightPanel.add(absentPanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("Theme")) {
          		rightPanel.add(themePanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("About")) {
          		rightPanel.add(aboutPanel, BorderLayout.CENTER);
          	} else if (str.equals("Network")) {
          		rightPanel.add(networkPanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("Block Network")) {
          		rightPanel.add(blockNetworkPanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	} else if (str.equals("Sound")) {
          		rightPanel.add(soundPanel, BorderLayout.CENTER);
          		rightPanel.add(applyPanel, BorderLayout.SOUTH);
          	}
          	System.out.println("rightPanel : " +  rightPanel.getComponentCount());
          	rightPanel.revalidate();
          	rightPanel.repaint();
		}
	};

	public JDialog getPrfDialog() {
		return prfDialog;
	}

	public void setPrfDialog(JDialog prfDialog) {
		this.prfDialog = prfDialog;
	}
	
}
